package com.yfqy.admin.resource;

import com.github.pagehelper.PageInfo;
import com.yfqy.admin.domain.dto.QueryById;
import com.yfqy.admin.domain.dto.promotion.ProductPromotionDTO;
import com.yfqy.admin.domain.dto.promotion.PromotionQueryDTO;
import com.yfqy.admin.domain.vo.PromotionVO;
import com.yfqy.admin.service.ProductPromotionService;
import com.yfqy.admin.domain.vo.common.ResultVO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品促销活动
 */
@RestController
@RequestMapping("/api/internal/promotion")
//@RequiredArgsConstructor
public class ProductPromotionController {

    @Resource
    private ProductPromotionService promotionService;

    @PostMapping("/create")
    public ResultVO<Void> createPromotion(@RequestBody @Valid ProductPromotionDTO dto) {
        boolean success = promotionService.createPromotion(dto);
        return success ? ResultVO.success() : ResultVO.fail("创建促销活动失败");
    }

    @PostMapping("/update")
    public ResultVO<Void> updatePromotion(@RequestBody @Valid ProductPromotionDTO dto) {
        boolean success = promotionService.updatePromotion(dto);
        return success ? ResultVO.success() : ResultVO.fail("更新促销活动失败");
    }

    @PostMapping("/end")
    public ResultVO<Void> endPromotion(@RequestBody @Valid QueryById queryById) {
        boolean success = promotionService.endPromotion(queryById.getId());
        return success ? ResultVO.success("手动结束促销活动成功") : ResultVO.fail("手动结束促销活动失败");
    }

    @PostMapping("/page")
    public ResultVO<PageInfo<PromotionVO>> queryPromotionPage(@RequestBody PromotionQueryDTO queryDTO) {
        PageInfo<PromotionVO> page = promotionService.queryPromotionPage(queryDTO);
        return ResultVO.data(page);
    }

    @GetMapping("/active/{spuId}")
    public ResultVO<List<PromotionVO>> getActivePromotionsBySpuId(@PathVariable Long spuId) {
        List<PromotionVO> promotions = promotionService.getActivePromotionsBySpuId(spuId);
        return ResultVO.data(promotions);
    }
}
